package com.wuwei.elearning.question.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.elearning.question.domain.Chapter;
import com.wuwei.elearning.question.domain.Question;
import com.wuwei.elearning.question.domain.Subject;
import com.wuwei.elearning.question.mapper.ChapterMapper;
import com.wuwei.elearning.question.mapper.SubjectMapper;
import com.wuwei.elearning.utils.result.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuwei
 * @since 2024-02-01
 */
@RestController
@RequestMapping("/elearning/chapter")
@CrossOrigin
public class ChapterController {

    @Resource
    private ChapterMapper chapterMapper;

    @Resource
    private SubjectMapper subjectMapper;

    @GetMapping("getBySubjectId/{subjectId}")
    public Result<List<Chapter>> getBySubjectId(@PathVariable("subjectId") Long subjectId){
        LambdaQueryWrapper<Chapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Chapter::getSubjectId,subjectId);

        List<Chapter> chapters = chapterMapper.selectList(queryWrapper);
        return Result.ok(chapters);
    }


    @GetMapping("getByName/{name}")
    public Result<List<Chapter>> getByName (@PathVariable("name") String name){
        LambdaQueryWrapper<Subject> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(name!=null&&!"".equals(name),Subject::getName,name);
        Subject subject = subjectMapper.selectOne(wrapper1);

        LambdaQueryWrapper<Chapter> wrapper = new LambdaQueryWrapper<>();
        List<Chapter> chapters = new ArrayList<>();
        if (subject!=null) {
            wrapper.eq(Chapter::getSubjectId,subject.getId());
            chapters = chapterMapper.selectList(wrapper);
        }
        return Result.ok(chapters);
    }

}

